﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TdolDemo_QBZ.Models.Task;
using TdolDemo_QBZ.Repository;

namespace TdolDemo_QBZ.Domain
{
    public class TdolTaskListDomain:ITdolTaskListDomain
    {
        ITdolTaskListRepository taskListRepository = new TdolTaskListRepository();
        //get list
        public List<TaskModel> GetTaskList(int taskId)
        {
            var list = new List<TaskModel>();
            list = taskListRepository.GetTaskList(taskId);
            return list;
        }
        //add
        public int AddTaskList(TaskModel task)
        {
            var taskid =  taskListRepository.AddTaskList(task);
            if (taskid > 0)
            {
                List<UnitModel> units = new List<UnitModel>();
                var timespan = (task.OpenIntervalEnd - task.OpenIntervalStart).Days;
                for (var i = 0; i <= timespan; i++)
                {
                    for (var j = 1; j < 4; j++)
                    {
                        var unit = new UnitModel
                        {
                            CreateBy = task.CreateBy,
                            CreateOn = task.CreateOn,
                            Guid = Guid.NewGuid(),
                            TaskId = taskid,
                            UnitType = j,
                            WorkUnitTime = task.OpenIntervalStart.AddDays(i)
                        };
                        units.Add(unit);
                    }
                }
                (new TdolUnitRepository()).AddUnit(units);
            }
            return taskid;
        }

        //delate
        public int DelateTaskList(int taskId)
        {
            return (new TdolTaskListRepository()).DelateTaskList(taskId);
        }
    }
}
